'use strict';
const db = uniCloud.database()
const foodRecord = db.collection('calorie_intake')
exports.main = async (event, context) => {
	//event为客户端上传的参数

	let pageSize = event.pageSize || 10
	let currentPage = event.currentPage || 1
	let params = event
	if (event.currentPage) {
		delete params.pageSize
		delete params.currentPage
	}
	let data = await foodRecord.where(params).skip(pageSize * (currentPage - 1)).limit(pageSize).orderBy('date',
		'desc').get()
	data.data.map(item => {
		let totalCaloric = 0
		if (item.breakfast && item.breakfast.length > 0) {
			item.breakfast.map(items => {
				totalCaloric += items.caloric
			})
		}
		if (item.lunch && item.lunch.length > 0) {
			item.lunch.map(items => {
				totalCaloric += items.caloric
			})
		}
		if (item.dinner && item.dinner.length > 0) {
			item.dinner.map(items => {
				totalCaloric += items.caloric
			})
		}
		item.totalCaloric = totalCaloric
	})
	//返回数据给客户端
	return {
		code: 200,
		data
	}
};